package com.xinchengxd.goods.controller;



import com.xinchengxd.commons.ResponseResult;
import com.xinchengxd.commons.entity.dto.GoodsNums;
import com.xinchengxd.commons.entity.goods.Goods;
import com.xinchengxd.commons.entity.goods.ShoppingCart;
import com.xinchengxd.goods.service.GoodsService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: 姚新成
 * @Date: 2021/11/9:0:08
 * @Description:
 */
@RequestMapping("/goods")
@RestController
public class GoodsController {
    @Resource
    GoodsService goodsService;

//    获取指定数量的商品
    @RequestMapping("/limit")
    public ResponseResult<List<Goods>> limit(Integer number){
        List<Goods> list =  goodsService.limit(number);
        return new ResponseResult<>(200,"ok",list);
    }

//    获取商品详情
    @RequestMapping("/findById")
    public ResponseResult<Goods> findById(Integer id){
        Goods goods = goodsService.findById(id);
        return new ResponseResult<>(200,"ok",goods);
    }

//    批量获取商品详情，使用list
    @RequestMapping("/findByList")
    public ResponseResult<List<Goods>> findByList(@RequestBody List<GoodsNums> item){
        System.out.println("接收到的item：" + item);
        List<Goods> goods = goodsService.findByList(item);
        return new ResponseResult<>(200,"ok",goods);
    }

//    减少库存
    @RequestMapping("stock")
    public ResponseResult<String> stock(Integer num,Integer id){
        int total = goodsService.stock(num,id);
        return total>0 ? new ResponseResult<>(200,"ok","修改成功")
                : new ResponseResult<>(601,"no","修改失败");
    }

//    获取购物车详情
    @RequestMapping("/car")
    public ResponseResult<List<ShoppingCart>> shoppingCart(@RequestHeader("userID") String id) {

//        获取user id
        Integer userId = null;
        try{
            userId = Integer.valueOf(id);
        }catch (NumberFormatException e){
            return new ResponseResult<>(501,"用户信息不正确",null);
        }
        List<ShoppingCart> shoppingCarts =  goodsService.findCart(userId);
        return new ResponseResult<>(200,"获取成功",shoppingCarts);

    }
}
